import os,re
import time
from flask import current_app

class executor_cmd():

    def sshpass_ttcmd(self,cmd):
        cmd=cmd.rstrip()
        cmd = '''sshpass -p {password} ssh -tt -o StrictHostKeyChecking=no -p 22 {user}@{hostIP} \"{cmd}\"'''.format(
                user=current_app.config.get("PIPE_USER"),
                password=current_app.config.get("PASSWORD"),
                prefix=current_app.config.get("PATH_PREFIX"),
                hostIP=current_app.config.get("HOST_IP"),
                cmd=cmd
            )
        os.popen(cmd)
        return cmd,202
    
    def sshpass_cmd(self,cmd):
        cmd=cmd.rstrip()
        cmd = '''sshpass -p {password} ssh -o StrictHostKeyChecking=no -p 22 {user}@{hostIP} \"{cmd}\"'''.format(
                user=current_app.config.get("PIPE_USER"),
                password=current_app.config.get("PASSWORD"),
                prefix=current_app.config.get("PATH_PREFIX"),
                hostIP=current_app.config.get("HOST_IP"),
                cmd=cmd
            )
        os.popen(cmd)
        return cmd,202
    
    def sshpass_return(self,cmd):
        cmd=cmd.rstrip()
        cmd = '''sshpass -p {password} ssh -o StrictHostKeyChecking=no -p 22 {user}@{hostIP} {cmd}'''.format(
                user=current_app.config.get("PIPE_USER"),
                password=current_app.config.get("PASSWORD"),
                prefix=current_app.config.get("PATH_PREFIX"),
                hostIP=current_app.config.get("HOST_IP"),
                cmd=cmd
            )
        os.popen(cmd).read()
        return cmd,202